package com.java.comprehensive.other.jvm;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import com.java.comprehensive.other.entity.Person;

/**
 * @Author 窦绍飞
 * @create 2022/11/2
 */
public class Demo3 {

	public static void main(String[] args) {
		Person p = new Person("张三", 18, "男");
		WeakReference<Person> weakPerson = new WeakReference<Person>(p);
		int i = 0;
		while (true) {
			if (weakPerson.get() != null) {
				i++;
				System.out.println("Object is alive for " + i + " loops - " + weakPerson);
			} else {
				System.out.println("Object has been collected.");
				break;
			}
		}

		List<Person> list = new ArrayList<>();
		Person p2 = new Person("张三2", 18, "男");
		Person p3 = new Person("张三3", 18, "男");
		Person p4 = new Person("张三4", 18, "男");
		list.add(p);
		list.add(p2);
		list.add(p2);
		list.add(p2);
	}
}
